<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 双大括号可以将实例的data的变量直接显示在页面上 -->
    <div id="app">
      <p>{{ message }}</p>
      <!-- @click添加点击事件 -->
      <button @click="handleClick">btn</button>

      <hr />

      <ul>
        <!-- v-for是vue的指令, 用于做循环渲染 -->
        <li v-for="item in list">
          {{ item.title }}的库存为{{ item.count }}件 -
          <!-- v-model用于做双向绑定 -->
          <input type="number" v-model="item.count" /> -
          <!-- v-if用于做条件渲染 -->
          <span v-if="+item.count === 0">卖完了</span>
        </li>
      </ul>

      <p>总计：{{ total }}</p>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      // createApp方法用于创建一个app实例
      const { createApp } = Vue;
      createApp({
        // data叫响应式数据
        data() {
          return {
            message: "hello 千锋大前端",
            list: [
              {
                title: "🍎",
                count: 3,
              },
              {
                title: "🍌",
                count: 5,
              },
              {
                title: "🍉",
                count: 6,
              },
            ],
          };
        },
        // methods里面写事件处理函数
        methods: {
          handleClick() {
            this.message = "123";
          },
        },
        // computed表示计算属性,里面写的是函数,必须要return
        computed: {
          total() {
            return this.list.reduce((sum, item) => {
              return (sum += +item.count);
            }, 0);
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
